//
//  FLMClaimOrderController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/15.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMClaimOrderController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "FLMTextFieldCell.h"
#import "FLMManageOrderDetailCell.h"
#import "ZLPhotoActionSheet.h"
@interface FLMClaimOrderController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSString *selectStr, *orderNoStr;
@property (nonatomic,strong) UIButton *imageButton;
@property (nonatomic,strong) NSMutableArray *selectArr;
@property (nonatomic,strong) UIImage *selectImage;

@end

@implementation FLMClaimOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"订单认领";
    self.selectArr = [@[@"1",@"2",@"3"]mutableCopy];
    [self layoutUI];
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightButton setTitleColor:black forState:UIControlStateNormal];
    [rightButton setTitle:@"确认" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = item;
    
}


- (void)layoutUI
{
    if (!_tableView) {
        _tableView = [[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[FLMTextFieldCell class] forCellReuseIdentifier:@"FLMTextFieldCell"];
        [_tableView registerClass:[FLMManageOrderDetailCell class] forCellReuseIdentifier:@"FLMManageOrderDetailCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [self addTableFooterView];
    }
    
}

- (void)addTableFooterView
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenW+60)];
    
    UIButton *imageButton = [[UIButton alloc]init];
    imageButton.backgroundColor = [UIColor lightGrayColor];
    [imageButton addTarget:self action:@selector(addImage) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:imageButton];
    [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@30);
        make.right.equalTo(@-30);
        make.height.equalTo(imageButton.mas_width);
        make.top.equalTo(footerView.mas_top).offset(30);
    }];
    
    _imageButton = imageButton;
    
    self.tableView.tableFooterView = footerView;
}

- (void)addImage
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置最大选择数量
    actionSheet.maxSelectCount = 1;
    //设置预览图最大数目
    actionSheet.maxPreviewCount = 20;
    __weak typeof(self) weakSelf = self;
    [actionSheet showWithSender:self animate:YES completion:^(NSArray<UIImage *> * _Nonnull selectPhotos) {
        if (selectPhotos.count) {
            [weakSelf.imageButton setImage:[selectPhotos firstObject] forState:UIControlStateNormal];
            _selectImage = selectPhotos.firstObject;
        }
    }];
}

- (NSMutableArray *)selectArr
{
    if (!_selectArr) {
        _selectArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _selectArr;
}

#pragma mark --UITableViewDelegate && UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        FLMTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMTextFieldCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.row ==1) {
        FLMManageOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLMManageOrderDetailCell"];
        [cell configClaimOrderWithContent:_selectStr];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
 
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==1) {
        
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for (NSString *selectStr in self.selectArr) {
            
                [sheet addButtonWithTitle:selectStr];
            }
            [sheet addButtonWithTitle:@"取消"];
        sheet.cancelButtonIndex = sheet.numberOfButtons-1;
        [sheet showInView:self.view];
        
    }
}

#pragma mark --UIActionSheet delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    _selectStr = buttonIndex>=self.selectArr.count?nil:self.selectArr[buttonIndex];
    [self.tableView reloadData];
}

#pragma mark - 私有方法

- (void)confirmAction
{
    
}

@end
